package com.xindaoapp.happypet.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.WindowManager;

import com.xindaoapp.happypet.application.HappyPetApplication;

public class MergeBitmap {

    public static Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
        Context c = HappyPetApplication.getInstance();
        // 获取屏幕尺寸大小，是程序能在不同大小的手机上有更好的兼容性
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        int screenwidth = wm.getDefaultDisplay().getWidth();// 手机屏幕的宽度
        int px = DensityUtil.dip2px(c, screenwidth) - (DensityUtil.dip2px(c, screenwidth)) / 10;
        // 获取图1，图2的宽高
        int f1_width = firstBitmap.getWidth();
        int f1_hight = firstBitmap.getHeight();
        int f2_width = secondBitmap.getWidth();
        int f2_hight = secondBitmap.getHeight();
        // 计算小图的偏移量
        float padding_x = (px - f2_width) / 2;
        float i = px / f1_width;
        float padding_y = (secondBitmap.getHeight() + 60 - f2_hight) / 2;

        Matrix matrix = new Matrix();
        matrix.setScale(px, (int) (firstBitmap.getHeight() * i));
        Bitmap bitmap =
                Bitmap.createBitmap(px, secondBitmap.getHeight() + 60, firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(secondBitmap, padding_x, padding_y, null);
        canvas.drawBitmap(firstBitmap, matrix, null);
        return bitmap;
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap =
                Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;

    }

}
